package com.achievement.service.impl;


import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.achievement.domain.dto.MagazineDTO;
import com.achievement.domain.entity.MagazineEntity;
import com.achievement.domain.param.MagazineParam;
import com.achievement.domain.vo.MagazineVO;
import com.achievement.mapper.MagazineMapper;
import com.achievement.mappings.MagazineSourceMapping;
import com.achievement.service.IMagazineService;
import com.achievement.service.IMagazineSourceService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author CNKI
* @description 针对表【dm_magazine(科研成果-期刊源列表-期刊列表)】的数据库操作Service实现
* @createDate 2025-02-28 14:53:32
*/
@Service
public class MagazineServiceImpl extends ServiceImpl<MagazineMapper, MagazineEntity>
    implements IMagazineService {

    @Autowired
    private MagazineSourceMapping mapping;

    @Autowired
    private IMagazineSourceService magazineSourceService;

    /**
     * 新增期刊
     *
     * @param dto dto
     */
    @Override
    public String add(MagazineDTO dto) {
        String UUID = IdUtil.randomUUID().replace("-", "");

        dto.setId(UUID);

        //新增刊物级别基本信息
        MagazineEntity entity = mapping.MagazineDto2MagazineEntity(dto);

        boolean save = this.save(entity);

        if (save) {
            return entity.getId();
        }

        return "";
    }

    /**
     * 删除期刊
     *
     * @param ids ids
     */
    @Override
    public boolean batchRemove(List<String> ids) {
        return this.removeByIds(ids);
    }

    /**
     * 修改期刊
     *
     * @param dto dto
     */
    @Override
    public boolean update(MagazineDTO dto) {
        MagazineEntity entity = mapping.MagazineDto2MagazineEntity(dto);

        return this.updateById(entity);
    }

    /**
     * 列表检索
     *
     * @param param param
     */
    @Override
    public List<MagazineVO> selectByPage(MagazineParam param) {
        List<MagazineVO> magazineVOList = this.baseMapper.getList(param);

        return magazineVOList;
    }

    /**
     * 查询刊物论文集名称
     *
     * @param param param
     */
    @Override
    public List<String> select(MagazineParam param) {
        LambdaQueryWrapper<MagazineEntity> queryWrapper = new LambdaQueryWrapper<>();

        String year = param.getYear();
        if (StrUtil.isBlank(year)) {
            return new ArrayList<>();
        }


        List<String> ids = magazineSourceService.selectIdsByYear(year);

        if (ids.isEmpty()) {
            return new ArrayList<>();
        }

        queryWrapper.in(MagazineEntity::getMagazineSourceId, ids);


        List<String> nameList = this.baseMapper.select(param, queryWrapper);

        return nameList;
    }

    /**
     * 期刊导出
     *
     * @param dto dto
     */
    @Override
    public List<MagazineEntity> getListByConditions(MagazineDTO dto) {
        LambdaQueryWrapper<MagazineEntity> queryWrapper = new LambdaQueryWrapper();
        List<MagazineEntity> list = this.list(queryWrapper);
        return list;
    }

    /**
     * 获取期刊详细信息
     *
     * @param id id
     */
    @Override
    public MagazineVO getInfoById(String id) {
        MagazineEntity entity = this.getById(id);
        if (ObjectUtil.isNull(entity)) {
            return new MagazineVO();
        }

        MagazineVO magazineVO = mapping.Magazineentity2MagazineVo(entity);

        return magazineVO;
    }
}




